Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Perl Programming Language

Can't modify constant item in scalar assignment


I have the follow script for printing out the Last Modification Time
of a file and it has been working on Solaris with Perl 5.6.1 and on
Redhat with Perl 5.8.0 without any problems.

fileLastModTm = localtime( (stat shift)[9] );
$fileLastModTm[4]++;  # month no. was zero-based, change that
$fileLastModTm[5] += 1900; # year no. was 1900-based, change that
fileLastModTm[ 5, 4, 3 ];  # spit it out, in yyyymmdd format

But somebody who needed the script could not use it, getting an error
such as follows:

Can't modify constant item in scalar assignment at flmtime.pl line 1,
near ");"
Can't use subscript on constant item at flmtime.pl line 4, near "3 ]"
Execution of flmtime.pl aborted due to compilation errors.

So, what went wrong? Now, I am asking the person to give me her perl's
version, but meanwhile, I think you clever bunch can still spot the
error for me? :) Thanks.

>>>>> "e" == emrefan  <dksle@hotmail.com> writes:

  e> I have the follow script for printing out the Last Modification Time
  e> of a file and it has been working on Solaris with Perl 5.6.1 and on
  e> Redhat with Perl 5.8.0 without any problems.

  e> fileLastModTm = localtime( (stat shift)[9] );

that isn't a perl variable. where is @?

  e> $fileLastModTm[4]++;  # month no. was zero-based, change that
  e> $fileLastModTm[5] += 1900; # year no. was 1900-based, change that
  e> fileLastModTm[ 5, 4, 3 ];  # spit it out, in yyyymmdd format

that isn't a perl variable. where is @?

  e> So, what went wrong? Now, I am asking the person to give me her perl's
  e> version, but meanwhile, I think you clever bunch can still spot the
  e> error for me? :) Thanks.

the code was butchered somehow as neither of those lines has a proper
perl variable.

besides that it would be better done with strftime.

uri

--
Uri Guttman  ------  u@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org

On Jun 5, 10:30 am, Uri Guttman <u@stemsystems.com> wrote:

I asure you that the whole script was quoted and nothing was butchered
off. I have not been an active perl programmer but I guess @_ (i.e.
@ARGV) was implied to the shift command? As I said, the script does
run fine on two platforms, that's why I am puzzled.
On 2007-06-05, emrefan <dksle@hotmail.com> wrote:

> On Jun 5, 10:30 am, Uri Guttman <u@stemsystems.com> wrote:
>> >>>>> "e" == emrefan  <dksle@hotmail.com> writes:

>>   e> fileLastModTm = localtime( (stat shift)[9] );

>> that isn't a perl variable. where is @?

>>   e> fileLastModTm[ 5, 4, 3 ];  # spit it out, in yyyymmdd format

>> that isn't a perl variable. where is @?

> I asure you that the whole script was quoted and nothing was butchered
> off.

Then the script is broken.  Fix it.  Your script should include

use strict;
use warnings;

which would have pointed out these obvious errors for you.

> I have not been an active perl programmer but I guess @_ (i.e.
> @ARGV) was implied to the shift command?

Irrelevant, as fileLastModTm is not a Perl variable, which is where both
your problems lie.

> As I said, the script does
> run fine on two platforms, that's why I am puzzled.

I'm skeptical, but it still doesn't matter, as the script as you posted
it is wrong (or at the very least poorly written).

You should consider reading the Posting Guidelines, which help you write
a post which is likely to get you a helpful response.

--keith

--
kkeller-use@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
see X- headers for PGP signature information

On Jun 5, 10:30 am, Uri Guttman <u@stemsystems.com> wrote:

Ah, I looked at the thing closely just now and boy what blunder did I
make! So a @ was missing from the 1st line and "print $" was missing
from the last line. Gosh, I never imagined a cut-n-paste job could be
messed up like that. But I was to blame too, I didn't look before I
jump, er, ask. Sorry for wasting the bandwidth and your time. I've
learnt something from this though, if that's any comfort - I will not
make such a silly mistake, in public especially, again.
At 2007-06-04 11:21PM, "emrefan" wrote:

>  On Jun 5, 10:30 am, Uri Guttman <u@stemsystems.com> wrote:
> > >>>>> "e" == emrefan  <dksle@hotmail.com> writes:
> >   e> fileLastModTm = localtime( (stat shift)[9] );
> >   e> $fileLastModTm[4]++;  # month no. was zero-based, change that
> >   e> $fileLastModTm[5] += 1900; # year no. was 1900-based, change that
> >   e> fileLastModTm[ 5, 4, 3 ];  # spit it out, in yyyymmdd format
[...]
>  Ah, I looked at the thing closely just now and boy what blunder did I
>  make! So a @ was missing from the 1st line and "print $" was missing
>  from the last line.

Note that this command:
    print $fileLastModTm[ 5, 4, 3 ];
is not going to:
    # spit it out, in yyyymmdd format

Array slices (the "[5,4,3]" construct) imply you want multiple items, so
you have to reference the variable name with "@".  You would want:
    printf "%4d%02d%02d", @fileLastModTm[ 5, 4, 3 ];

However, you can avoid the month/year adjustments altogether with:
    use POSIX 'strftime';
    my $fileModDate = strftime('%Y%m%d', localtime( (stat shift)[9] ));

--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc