Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

( #192722=categorized question: print w/ replies, xml ) Need Help??
Contributed by @ncientgoose on Aug 25, 2002 at 22:01 UTC
Q&A  > subroutines


Description:

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.

Answer: Why does my subroutine not receive the scalar parameter as $_?
contributed by rob_au

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;

Answer: Why does my subroutine not receive the scalar parameter as $_?
contributed by BrowserUk

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.

Answer: Why does my subroutine not receive the scalar parameter as $_?
contributed by BrowserUk

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

See perlman:perlsub Description for explanation.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (11)
    As of 2015-07-29 19:28 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (267 votes), past polls