[31865] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 3128 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Sep 11 21:09:29 2010

Date: Sat, 11 Sep 2010 18:09:14 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sat, 11 Sep 2010     Volume: 11 Number: 3128

Today's topics:
        How to pass hash as sub parameter? <rui.maciel@gmail.com>
    Re: How to pass hash as sub parameter? <sreservoir@gmail.com>
    Re: How to pass hash as sub parameter? <jurgenex@hotmail.com>
    Re: How to pass hash as sub parameter? sln@netherlands.com
    Re: How to pass hash as sub parameter? <rui.maciel@gmail.com>
    Re: How to pass hash as sub parameter? (Randal L. Schwartz)
    Re: How to pass hash as sub parameter? <jurgenex@hotmail.com>
    Re: How to pass hash as sub parameter? <skye.shaw@gmail.com>
    Re: How to pass hash as sub parameter? <rui.maciel@gmail.com>
    Re: How to pass hash as sub parameter? <ben@morrow.me.uk>
    Re: How to pass hash as sub parameter? <jurgenex@hotmail.com>
    Re: How to pass hash as sub parameter? <rui.maciel@gmail.com>
    Re: How to pass hash as sub parameter? <rui.maciel@gmail.com>
    Re: How to pass hash as sub parameter? <tadmc@seesig.invalid>
    Re: How to pass hash as sub parameter? <uri@StemSystems.com>
    Re: How to pass hash as sub parameter? <rui.maciel@gmail.com>
    Re: How to pass hash as sub parameter? <rui.maciel@gmail.com>
    Re: How to pass hash as sub parameter? <uri@StemSystems.com>
    Re: How to pass hash as sub parameter? <uri@StemSystems.com>
    Re: How to pass hash as sub parameter? <jurgenex@hotmail.com>
    Re: How to pass hash as sub parameter? (Randal L. Schwartz)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sat, 11 Sep 2010 14:52:57 +0100
From: Rui Maciel <rui.maciel@gmail.com>
Subject: How to pass hash as sub parameter?
Message-Id: <4c8b89bb$0$24474$a729d347@news.telepac.pt>

I want to pass a hash to a sub through as a sub parameter.  In order to do so I've written the 
following sub:


<code>
sub print_http(;%)
{
        if(@_ != 0)
        {
                my (%params) = $_[0];  # line 15
                print "Content-Type: ";
                exists $params{"type"} ? print $params{"type"}: print "text/html";  #line 17
                print "; ";
                exists $params{"charset"} ? print $params{"charset"}: print "charset=ISO-8859-1";
        }
        else
        {
                print "Content-Type: text/html; charset=ISO-8859-1";
        }
        print "\n\n";
}
</code>


Yet, it throws the following error messages:

test.pl: Odd number of elements in hash assignment at ./index.pl line 15.
test.pl: Use of uninitialized value $params{"type"} in print at ./index.pl line 17.


What am I doing wrong?  And what's the best way to pass a hash to a sub?


Thanks in advance,
Rui Maciel


------------------------------

Date: Sat, 11 Sep 2010 10:33:17 -0400
From: sreservoir <sreservoir@gmail.com>
Subject: Re: How to pass hash as sub parameter?
Message-Id: <i6g3vj$mpg$1@news.eternal-september.org>

On 9/11/2010 9:52 AM, Rui Maciel wrote:
> I want to pass a hash to a sub through as a sub parameter.  In order to do so I've written the
> following sub:
>
>
> <code>
> sub print_http(;%)
> {
>          if(@_ != 0)
>          {
>                  my (%params) = $_[0];  # line 15
>                  print "Content-Type: ";
>                  exists $params{"type"} ? print $params{"type"}: print "text/html";  #line 17
>                  print "; ";
>                  exists $params{"charset"} ? print $params{"charset"}: print "charset=ISO-8859-1";
>          }
>          else
>          {
>                  print "Content-Type: text/html; charset=ISO-8859-1";
>          }
>          print "\n\n";
> }
> </code>
>
>
> Yet, it throws the following error messages:
>
> test.pl: Odd number of elements in hash assignment at ./index.pl line 15.
> test.pl: Use of uninitialized value $params{"type"} in print at ./index.pl line 17.
>
>
> What am I doing wrong?  And what's the best way to pass a hash to a sub?

% doesn't actually check parameters. you'll have to use carp and do
checking manually.

-- 

  "Six by nine. Forty two."
  "That's it. That's all there is."
  "I always thought something was fundamentally wrong with the universe."


------------------------------

Date: Sat, 11 Sep 2010 08:08:44 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: How to pass hash as sub parameter?
Message-Id: <gv5n861ddm0do4ctt6pvpl07srerkr4gmd@4ax.com>

Rui Maciel <rui.maciel@gmail.com> wrote:
>I want to pass a hash to a sub through as a sub parameter.  In order to do so I've written the 
>following sub:
[...]
>                my (%params) = $_[0];  # line 15

$_[0] is a single scalar ...

>Yet, it throws the following error messages:
>
>test.pl: Odd number of elements in hash assignment at ./index.pl line 15.

 ... and a single scalar is certainly an odd number of elements.

>What am I doing wrong?  

Are you passing a hash ref or the hash itself as argument? In the first
case dereference $_[0] before assigning to %params, in the second case
just assign the whole array @_ to %params.
Either way, assigning just the first element is wrong.

>And what's the best way to pass a hash to a sub?

There is no single answer to that question, both methods are valid. It
really depends on what you want to do, e.g. modify the hash or the
content of the hash, how large is the hash, are there other parameters
for the sub, ...

jue

jue


------------------------------

Date: Sat, 11 Sep 2010 08:21:10 -0700
From: sln@netherlands.com
Subject: Re: How to pass hash as sub parameter?
Message-Id: <237n86181r5i6mfsoiajdndspbe8n6nkak@4ax.com>

On Sat, 11 Sep 2010 14:52:57 +0100, Rui Maciel <rui.maciel@gmail.com> wrote:

>I want to pass a hash to a sub through as a sub parameter.
You don't pass a hash, either a reference to one, or a listized hash.

>  In order to do so I've written the 
>following sub:
>
>
><code>
>sub print_http(;%)
You prototype here ...

>{
>        if(@_ != 0)
But here, if @_ is empty, a prototype mismatch message appears
>        {
>                my (%params) = $_[0];  # line 15
>                print "Content-Type: ";
>                exists $params{"type"} ? print $params{"type"}: print "text/html";  #line 17
>                print "; ";
>                exists $params{"charset"} ? print $params{"charset"}: print "charset=ISO-8859-1";
>        }
>        else
>        {
>                print "Content-Type: text/html; charset=ISO-8859-1";
>        }
>        print "\n\n";
>}
></code>
>
>
>Yet, it throws the following error messages:
>
>test.pl: Odd number of elements in hash assignment at ./index.pl line 15.
>test.pl: Use of uninitialized value $params{"type"} in print at ./index.pl line 17.
>
>
>What am I doing wrong?  And what's the best way to pass a hash to a sub?
>
>
>Thanks in advance,
>Rui Maciel

If you are going to prototype and accept the possible mismatch message,
you have to check for the eveness or accept the warning or:

sub print_http(;%){
    if (@_%2) {
      print "not even-sized list\n";
      return;
   }
   ...
}

Or, without a prototype is better:
sub print_http {
    if (@_%2) {
      print "not even-sized list\n";
      return;
   }
   ...
}

The other option is to pass a hash reference.

-sln


------------------------------

Date: Sat, 11 Sep 2010 22:39:38 +0100
From: Rui Maciel <rui.maciel@gmail.com>
Subject: Re: How to pass hash as sub parameter?
Message-Id: <4c8bf71f$0$24473$a729d347@news.telepac.pt>

sreservoir wrote:

> % doesn't actually check parameters. you'll have to use carp and do
> checking manually.

Now that's a turn-off.  I was under the impression that providing a function prototype would be 
enough for this.  So how is it possible to write a Perl sub which enforces the types of it's 
arguments?


Rui Maciel


------------------------------

Date: Sat, 11 Sep 2010 15:08:31 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: How to pass hash as sub parameter?
Message-Id: <86aanodjc0.fsf@red.stonehenge.com>

>>>>> "Rui" == Rui Maciel <rui.maciel@gmail.com> writes:

Rui> Now that's a turn-off.  I was under the impression that providing a
Rui> function prototype would be enough for this.  So how is it possible
Rui> to write a Perl sub which enforces the types of it's arguments?

Yes.  Use MooseX::Declare.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion


------------------------------

Date: Sat, 11 Sep 2010 15:30:28 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: How to pass hash as sub parameter?
Message-Id: <6k0o86t08a9vasjtlg9s2ml2a9fl2btjru@4ax.com>

Rui Maciel <rui.maciel@gmail.com> wrote:
>sreservoir wrote:
>
>> % doesn't actually check parameters. you'll have to use carp and do
>> checking manually.
>
>Now that's a turn-off.  I was under the impression that providing a function prototype would be 
>enough for this.  So how is it possible to write a Perl sub which enforces the types of it's 
>arguments?

The only type of argument you can pass to a Perl sub are scalars which
are passed as a flat list.
Therefore your question is meaningless: there is one and only one type
of argument.

jue


------------------------------

Date: Sat, 11 Sep 2010 15:32:27 -0700 (PDT)
From: "Skye Shaw!@#$" <skye.shaw@gmail.com>
Subject: Re: How to pass hash as sub parameter?
Message-Id: <a16d303f-df92-4986-b8e2-71bfd5fef616@z30g2000prg.googlegroups.com>

On Sep 11, 2:39=A0pm, Rui Maciel <rui.mac...@gmail.com> wrote:
> sreservoir wrote:
> > % doesn't actually check parameters. you'll have to use carp and do
> > checking manually.
>
> Now that's a turn-off. =A0I was under the impression that providing a fun=
ction prototype would be
> enough for this. =A0So how is it possible to write a Perl sub which enfor=
ces the types of it's
> arguments?

die "usage: print_http(HASH)" unless @_ and @_ % 2 =3D=3D 0;
die "usage: print_http(ARRAYREF)" unless isa $_[0], 'ARRAY';

There's Params::Validate (http://search.cpan.org/~drolsky/Params-
Validate-0.95/lib/Params/Validate.pm)
though I think it is overkill for simple cases like yours.



------------------------------

Date: Sat, 11 Sep 2010 23:51:09 +0100
From: Rui Maciel <rui.maciel@gmail.com>
Subject: Re: How to pass hash as sub parameter?
Message-Id: <4c8c07e3$0$24463$a729d347@news.telepac.pt>

sln@netherlands.com wrote:

> You don't pass a hash, either a reference to one, or a listized hash.

Thanks for the help, sln.  I was left with the impression that @_ stored a list of parameters in the 
sense that if someone passed a set of arrays to a sub then @_ would be a list of lists.  So, passing 
two lists as parameters means that the sub only gets a long, concatenated list?


Thanks for the help,
Rui Maciel




------------------------------

Date: Sun, 12 Sep 2010 00:03:09 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: How to pass hash as sub parameter?
Message-Id: <deetl7-8sd.ln1@osiris.mauzo.dyndns.org>


Quoth "Skye Shaw!@#$" <skye.shaw@gmail.com>:
>
> die "usage: print_http(ARRAYREF)" unless isa $_[0], 'ARRAY';

Do *not* do that. Quite apart from needing to import 'isa' from
UNIVERSAL, which is a really bad idea all by itself, calling ->isa as a
function is always wrong. You want Scalar::Util::reftype (and possibly
Scalar::Util::blessed, if you care).

In most cases it would be better to just array-deref it and let that
catch the error. For one thing, it will correctly handle @{}-overloaded
objects, which are otherwise rather hard to identify.

> There's Params::Validate (http://search.cpan.org/~drolsky/Params-
> Validate-0.95/lib/Params/Validate.pm)
> though I think it is overkill for simple cases like yours.

It is, however, *correct*, and as you have just demonstrated correctness
here is not as easy as it might seem.

Ben



------------------------------

Date: Sat, 11 Sep 2010 16:58:47 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: How to pass hash as sub parameter?
Message-Id: <7k5o86tsmf77mjgb7ppet5oijjfj0cebi9@4ax.com>

Rui Maciel <rui.maciel@gmail.com> wrote:
>sln@netherlands.com wrote:
>
>> You don't pass a hash, either a reference to one, or a listized hash.
>
>Thanks for the help, sln.  

Oh, is that idiot still around? I had no idea....

>I was left with the impression that @_ stored a list of parameters in the 
>sense that if someone passed a set of arrays to a sub then @_ would be a list of lists.  So, passing 
>two lists as parameters means that the sub only gets a long, concatenated list?

perldoc perlsub:

  The Perl model for function call and return values is simple: all
  functions are passed as parameters one single flat list of scalars[..]
Any arrays or hashes in these call and return lists will
  collapse, losing their identities--[...]

If you know of a way how to word this even more explicit then please let
us know.

jue


------------------------------

Date: Sun, 12 Sep 2010 00:51:38 +0100
From: Rui Maciel <rui.maciel@gmail.com>
Subject: Re: How to pass hash as sub parameter?
Message-Id: <4c8c160a$0$24466$a729d347@news.telepac.pt>

J�rgen Exner wrote:

> The only type of argument you can pass to a Perl sub are scalars which
> are passed as a flat list.
> Therefore your question is meaningless: there is one and only one type
> of argument.

If it is meaningless then how do you explain Perl's function prototyping and it's explicit support 
for array, hash, code and glob parameters?


Rui Maciel


------------------------------

Date: Sun, 12 Sep 2010 01:02:40 +0100
From: Rui Maciel <rui.maciel@gmail.com>
Subject: Re: How to pass hash as sub parameter?
Message-Id: <4c8c18a0$0$24469$a729d347@news.telepac.pt>

Randal L. Schwartz wrote:

>>>>>> "Rui" == Rui Maciel <rui.maciel@gmail.com> writes:
> 
> Rui> Now that's a turn-off.  I was under the impression that providing a
> Rui> function prototype would be enough for this.  So how is it possible
> Rui> to write a Perl sub which enforces the types of it's arguments?
> 
> Yes.  Use MooseX::Declare.

Thanks for the tip, Randal.  It looks promising.  Nevertheless, I've never heard of Moose before, 
which may quite possibly be due to the fact that I'm practically just starting out.  So, do you 
happen to know if Mosse has been widely adopted or is it an obscure feature of Perl?


Thanks for the help,
Rui Maciel


------------------------------

Date: Sat, 11 Sep 2010 19:15:25 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: How to pass hash as sub parameter?
Message-Id: <slrni8o6sq.kk1.tadmc@tadbox.sbcglobal.net>

Rui Maciel <rui.maciel@gmail.com> wrote:

> I was left with the impression that @_ stored a list of parameters in the 
> sense that if someone passed a set of arrays to a sub then @_ would be a list of lists.  So, passing 
> two lists as parameters means that the sub only gets a long, concatenated list?


Can it be that you have not read even as far as the 2nd paragraph
of the description of Perl's subroutines?

    perldoc perlsub

    ...

    The Perl model for function call and return values is simple: all
    functions are passed as parameters one single flat list of scalars, and
    all functions likewise return to their caller one single flat list of
    scalars.  Any arrays or hashes in these call and return lists will
    collapse, losing their identities--but you may always use
    pass-by-reference instead to avoid this.


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.


------------------------------

Date: Sat, 11 Sep 2010 20:32:58 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: How to pass hash as sub parameter?
Message-Id: <87mxrng5s5.fsf@quad.sysarch.com>

>>>>> "RM" == Rui Maciel <rui.maciel@gmail.com> writes:

  RM> J�rgen Exner wrote:
  >> The only type of argument you can pass to a Perl sub are scalars which
  >> are passed as a flat list.
  >> Therefore your question is meaningless: there is one and only one type
  >> of argument.

  RM> If it is meaningless then how do you explain Perl's function
  RM> prototyping and it's explicit support for array, hash, code and
  RM> glob parameters?

because you haven't read the docs enough or correctly. prototypes are
misnamed and not what you seem to think they are. they are NOT for
verifying types of arguments passed to subs. they are mostly useful for
converting some types to known types. it allows you to declare a sub
that can look like a builtin - e.g. a push which takes an array (not an
array ref) as it first arg. normally a sub can't do that.

so stop with your ranting about what you think perl prototypes do and
learn what they actually do. most decent perl hackers avoid prototypes
and only use them when they do something important. checking param types
is done in other ways.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


------------------------------

Date: Sun, 12 Sep 2010 01:29:42 +0100
From: Rui Maciel <rui.maciel@gmail.com>
Subject: Re: How to pass hash as sub parameter?
Message-Id: <4c8c1ef6$0$24461$a729d347@news.telepac.pt>

Tad McClellan wrote:

> Can it be that you have not read even as far as the 2nd paragraph
> of the description of Perl's subroutines?

If you happen to take the time to google "perl sub tutorial" you will learn that the first hit from 
perl.org does not rank very high.


Rui Maciel


------------------------------

Date: Sun, 12 Sep 2010 01:33:46 +0100
From: Rui Maciel <rui.maciel@gmail.com>
Subject: Re: How to pass hash as sub parameter?
Message-Id: <4c8c1fea$0$24461$a729d347@news.telepac.pt>

J�rgen Exner wrote:

> If you know of a way how to word this even more explicit then please let
> us know.

Do you believe you are doing anyone any favours by posting condescending messages? 


Rui Maciel





------------------------------

Date: Sat, 11 Sep 2010 20:41:28 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: How to pass hash as sub parameter?
Message-Id: <8739tfg5dz.fsf@quad.sysarch.com>

>>>>> "RM" == Rui Maciel <rui.maciel@gmail.com> writes:

  RM> Tad McClellan wrote:
  >> Can it be that you have not read even as far as the 2nd paragraph
  >> of the description of Perl's subroutines?

  RM> If you happen to take the time to google "perl sub tutorial" you
  RM> will learn that the first hit from perl.org does not rank very
  RM> high.

and if you use google to learn programming, you must be very high!

perl has a very solid set of docs that come with it. you don't need
google to find them. try reading them and learn about perl's subs before
you go off on rants like this.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


------------------------------

Date: Sat, 11 Sep 2010 20:42:25 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: How to pass hash as sub parameter?
Message-Id: <87y6b7eqry.fsf@quad.sysarch.com>

>>>>> "RM" == Rui Maciel <rui.maciel@gmail.com> writes:

  RM> J�rgen Exner wrote:
  >> If you know of a way how to word this even more explicit then please let
  >> us know.

  RM> Do you believe you are doing anyone any favours by posting
  RM> condescending messages?

pot meet kettle.

you have not been gracious at all in asking for help nor in how you
reply to answers. me thinks you are not going to do well in the
programming world with an attitude like that.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


------------------------------

Date: Sat, 11 Sep 2010 17:57:50 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: How to pass hash as sub parameter?
Message-Id: <h99o86lassqqnpc1t64d37qco48hil0umn@4ax.com>

Rui Maciel <rui.maciel@gmail.com> wrote:
>J?rgen Exner wrote:
>
>> If you know of a way how to word this even more explicit then please let
>> us know.
>
>Do you believe you are doing anyone any favours by posting condescending messages? 

Obviously you did not understand that documentation, therefore I  repeat
my request: if you know of a better way to describe that arguments for
Perl subs are flat lists then please let us know.

jue


------------------------------

Date: Sat, 11 Sep 2010 17:56:53 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: How to pass hash as sub parameter?
Message-Id: <86vd6bdbje.fsf@red.stonehenge.com>

>>>>> "Rui" == Rui Maciel <rui.maciel@gmail.com> writes:

Rui> So, do you happen to know if Mosse has been widely adopted or is it
Rui> an obscure feature of Perl?

Yes.

:-)

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 3128
***************************************


home help back first fref pref prev next nref lref last post