Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Function Prototypes

by hippo (Bishop)
on Nov 24, 2015 at 13:21 UTC ( [id://1148501]=note: print w/replies, xml ) Need Help??


in reply to Function Prototypes

Your args as in the example are a hash, so in the absence of any indication of why you want to use prototypes the obvious works for me:

#!/usr/bin/perl -w use strict; my @aFieldHeaders; my %hFieldConfigs; sub ConfigRead (%); my $sConfigFile = 'mydata.csv'; ConfigRead(sFILE => $sConfigFile, apHEAD => \@aFieldHeaders, hpFIELDCONF => \%hFieldConfigs); # my array and hash now have some cool stuff in 'em! sub ConfigRead (%) { my %hArgs = ( sFILE => 'BAD', # lazy user alert! whine & die apHEAD => 'BAD', # ^^^ see comment above hpFIELDCONF => 'BAD', # by now you know the drill! @_, # user demands come from here ); # stick some cool stuff in their variables }

But as everyone else has said: don't do that unless you have a good reason.

Replies are listed 'Best First'.
Re^2: Function Prototypes
by KimberTLE (Initiate) on Nov 24, 2015 at 18:19 UTC
    You, hippo, are my hero! And now that I see it, it makes perfect sense. Occam's Razor, after a good stropping. As promised: THANK YOU!
        Thanks! Own it, and I've read it. But messing with "named arguments" raised the question in my mind, "how would you prototype something like this?" And after too many sleepless millennia, I relented and had to ask! But kind of like...
        Paris in the the spring time
        Once you've written it, the correct answer eludes you forever. Again, thanks for supplying the two-by-four. Greatly appreciated!

      I still do not understand your original reasons for wanting to use prototypes, nor do I know if hippo's reply has dissuaded you, but if you are not dissuaded, here's an example of why prototypes are not useful as you seem to wish to use them:

      c:\@Work\Perl\monks>perl -wMstrict -le "use Data::Dump qw(dd); ;; sub S_ { my %h = @_; dd \%h; } sub Sh (%) { my %h = @_; dd \%h; } sub Sa (@) { my %h = @_; dd \%h; } ;; S_(qw(a b c)); Sh(qw(a b c)); Sa(qw(a b c)); " Odd number of elements in hash assignment at -e line 1. { a => "b", c => undef } Odd number of elements in hash assignment at -e line 1. { a => "b", c => undef } Odd number of elements in hash assignment at -e line 1. { a => "b", c => undef }
      The warning is in the  'misc' class, and escalating such warnings to FATALity would at least prevent the code from soldiering on regardless, but of course that has nothing to do with the prototype.


      Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-03-29 14:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found