Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Modifying Global Variable

by Willman023 (Scribe)
on Jan 08, 2003 at 17:19 UTC ( [id://225301]=perlquestion: print w/replies, xml ) Need Help??

Willman023 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Fellow Monks,

I'm having quite a problem, using a function to alter a global variable. I'm hoping you guys can show me how to use a function to modify multiple variables. I'm trying to convert (YYYY-MM-DD HH:MM:SS) to unix time(seconds starting from 1970). I'm thinking about using Time::Local for the conversion. The error I'm getting before even trying to convert is:

Can't modify constant item in scalar assignment at calendar.pl line 276, near ""$year.$month.$day";"
Execution of calendar.pl aborted due to compilation errors.

Code:

my $date; dateconverter($date_begin); print $date, "\n"; sub dateconverter { my $year, my $month, my $day, my $hour, my $min, my $sec; ($year, $month, $day) = split /-/, $_; ($day, $hour) = split / /, $day; ($hour, $min, $sec) = split /:/, $hour; main::date = "$year.$month.$day"; # get unix seconds for $date return main::date; }
I originally tryed just setting the date by using $date = "$year..."; but read one of Ovid's nodes on scope and tried main::date with no luck. Thanks alot in advance for any advice!

bW

Perl - the breakfast of Champions!

Replies are listed 'Best First'.
(z) Re: Modifying Global Variable
by zigdon (Deacon) on Jan 08, 2003 at 17:38 UTC

    There's a few problems with your code:

    • Instead of "main::date" try "$date" - you shouldn't need to specify main:: unless you specified a different package.
    • You are returning a value (the literal string "main::date"), but thankfully, you're not using it.
    • You are splitting on an uninitialised variable ($_), which you would have known if you had warnings turned on. Use $_[0] instead.
    • You can change all the splits into one split:
      my ($year, $month, $day, $hour, $min, $sec) = split /[- :]/, $_[0];
    • You don't need to modify the global - just return the value you need:
      my $date = dateconverter($date_begin); print $date, "\n"; sub dateconverter { my @bits = split /[- :]/, $_[0]; return join ".", @bits[0,1,2]; }

    -- Dan

Re: Modifying Global Variable
by gryphon (Abbot) on Jan 08, 2003 at 18:02 UTC

    Greetings Willman023

    If you want to just convert from your YYYY-MM-DD HH:MM:SS format into UNIX time, then why not just use CPAN's Date::Parse?

    use Date::Parse; print str2time('2003-01-08 09:55:36'), "\n";

    Or if you want to just save the datetime into $date:

    use Date::Parse; my $date = str2time('2003-01-08 09:55:36');

    gryphon
    code('Perl') || die;

Re: Modifying Global Variable
by boo_radley (Parson) on Jan 08, 2003 at 18:53 UTC
    Can't modify constant item in scalar assignment at calendar.pl line 276, near ""$year.$month.$day";"
    This error is caused because you've forgotten the glyph to main::date -- it should be $main::date. Without the dollar sign, it's not considered a variable.
    You should be wary of using a global variable for a return value, too. Are you really sure you want to change $main::date every time you want convert to epoch time? This sort of side effect, as well as the logic behind omitting a local declaration, should be documented to whatever standards you have.
Re: Modifying Global Variable
by vek (Prior) on Jan 08, 2003 at 17:41 UTC
    I think you're a little confused on how to return data from subroutines. I think this is what you want:
    my $date = dateconverter($date_begin); print $date, "\n"; sub dateconverter { my $dateFoo = shift; # do stuff here, # store your end result in $somevar return $somevar; # returns the value }
    Your $date var will now contain the value of $somevar.

    -- vek --

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://225301]
Approved by vek
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-04-23 11:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found