Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Why does my subroutine not receive the scalar parameter as $_?

by @ncientgoose (Novice)
on Aug 25, 2002 at 22:01 UTC ( [id://192722]=perlquestion: print w/replies, xml ) Need Help??

@ncientgoose has asked for the wisdom of the Perl Monks concerning the following question: (subroutines)

The problem is I am clearly passing a scalar var to this sub and it is getting a null list. I have tested this thoroughly. Why might it do this? The sub code is in a .pm package, but everything is clearly defined. I have similar subs and calls that work fine!
Here is my call where $form_data{'judeo_date'} is a date like "25-AUG-2002-AD":
my $j_date = $form_data{'judeo_date'}; my $long_count = get_long_count $j_date if defined $form_data{'judeo_d +ate'};
and
sub get_long_count { my ($dd, $mon, $yyyy, $bc) = split /-/, $_, 4; $yyyy = (0 - $yyyy) if ($bc eq 'BC'); my $feb = 0; if ( int ( $yyyy / 4 ) == ( $yyyy / 4 ) ) { $feb += 29; } else { $feb += 28; } my $months = { 'JAN' => '31', 'FEB' => "$feb", 'MAR' => '31', 'APR' => '30', 'MAY' => '31', 'JUN' => '30', 'JUL' => '31', 'AUG' => '31', 'SEP' => '30', 'OCT' => '31', 'NOV' => '30', 'DEC' => '31', }; my @month_keys = (keys %months); my $tot_days = $dd; for ($i = 0; $month_keys[$i] ne $mon; $i++) { $tot_days += $months{$month_keys[$i]}; } for ($y = $yyyy; $y >= -3114; $y--) { if ( int ( $y / 4 ) == ( $y / 4 ) ) { $tot_days += 366; } else { $tot_days += 365; } } $tot_days -= 223; my $kin = 0; my $uinal = 0; my $tun = 0; my $katun = 0; my $baktun = 0; $baktun = int ( $tot_days / 144000 ) if ( $tot_days >= 144000 ); $tot_days -= ( $baktun * 144000 ); $katun = int ( $tot_days / 7200 ) if ( $tot_days >= 7200 ); $tot_days -= ( $katun * 7200 ); $tun = int ( $tot_days / 360 ) if ( $tot_days >= 360 ); $tot_days -= ( $tun * 360 ); $uinal = int ( $tot_days / 20 ) if ( $tot_days >= 20 ); $tot_days -= ( $uinal * 20 ); $kin = $tot_days; my $long_count; $long_count = "$baktun.$katun.$tun.$uinal.$kin"; }
Thanks,
Chris

P.S. in case your wondering, this sub translates Judeo dates into Mayan long count calendar dates.

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: Why does my subroutine not receive the scalar parameter as $_?
by rob_au (Abbot) on Aug 25, 2002 at 22:35 UTC
    You are so close on this one - From perlsub ...

    Any arguments passed to the routine come in as the array @_. Thus if you called a function with two arguments, those would be stored in $_[0] and $_[1]. The array @_ is a local array, but its elements are aliases for the actual scalar parameters.

    So, if you are wanting to access the passed parameters directly, you will want to use $_[0] or alternately shift off values from the @_ array. eg.

    sub get_long_count { my ($dd, $mon, $yyyy, $bc) = split /-/, $_[0], 4;

Re: Why does my subroutine not receive the scalar parameter as $_?
by BrowserUk (Patriarch) on Aug 25, 2002 at 23:03 UTC

    In addition, you have trouble brewing once you fix that problem. You are defining your $months hash as a hash reference instead of a hash, and that breaks in the loop that follows. The relevant part of you code (with corrections) is:

    my %months = ( ## Change $ to % and { to ( --- no reason t +o use a reference here. 'JAN' => '31', 'FEB' => "$feb", 'MAR' => '31', 'APR' => '30', 'MAY' => '31', 'JUN' => '30', 'JUL' => '31', 'AUG' => '31', 'SEP' => '30', 'OCT' => '31', 'NOV' => '30', 'DEC' => '31', ); ## Changed { to ) my @month_keys = (keys %months); my $tot_days = $dd; for (my $i = 0; $month_keys[$i] ne $mon; $i++) { # Added my

    Also, in future, it is better to ask your questions in the Seeker of Perl Wisdom section rather than here in the Q&A section. This is reserved for generic Q&A's, rather than specific questions like your.

      Actually this is one of the best Q&'A's I've seen. If you think about it, even as an experienced perl person, it's a good question. One could quite reasonably expect this behavior. And of course the answer is...
      local $_ = shift || $_;
      Which I found myself using a bit recently.

      --
      perl -pew "s/\b;([mnst])/'$1/g"

Re: Why does my subroutine not receive the scalar parameter as $_?
by BrowserUk (Patriarch) on Aug 25, 2002 at 22:32 UTC

    Use $_[0] (the first element of @_) instead of $_.

    See perlman:perlsub Description for explanation.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-19 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found