Re: lambda array
[email protected] (Andrew Solomon) Sat, 13 Jan 2024 17:41:10 +0000
| Newsgroups | perl.beginners |
|---|---|
| Message-ID | <CAN=twKsDZwkGmTrBKcYZrLC6nfoYyA8mJBA5c0hXuaAcK4gJiA@mail.gmail.com> |
> when is an array in perl declared with [] instead of ()?
Using the [ ] delimiters you are creating a *reference* to an array. (Think
of a reference as the memory address where the array is stored). So
my $foo = [1,2,3];
is equivalent to the following, because given an array the \ gets the
reference to that array:
my @bar = (1,2,3);
my $foo = \@bar;
> why aren't arrays created the same way when being passed as parameters to
functions (when signatures are being used)?
The signatures aren't doing much other than automatically assigning parts
of @_ to variables declared in the signature.
(For more detail, see
https://www.effectiveperlprogramming.com/2015/04/use-v5-20-subroutine-signatures/
)
In the code you provided:
sub reply_multi ( $xmpp_o, $rcpts, $msg ) {
the argument $rcpts is expected to be a *reference* to an array, rather
than an array. It's a reference because in this line:
foreach my $rcpt (@$rcpts) {
@$rcpts is "dereferencing" to get the array.
> Parentheses should take precendence, shouldn't they?
They don't. The array is flattened into the list of arguments which means
that:
reply_multi( \$daemon{xmpp_o}, \($adminuser{fromJID}, $fromJID), "blah" );
becomes
reply_multi( \$daemon{xmpp_o}, \$adminuser{fromJID}, \$fromJID, "blah" );
resulting in the error:
"Too many arguments for subroutine 'main::reply_multi' (got 4; expected 3)".
A
On Sat, Jan 13, 2024 at 5:03 PM hw <[email protected]> wrote:
> On Sat, 2024-01-13 at 15:00 +0000, Andrew Solomon wrote:
> > I think the line:
> >
> > reply_multi( \$daemon{xmpp_o}, \($adminuser{fromJID}, $fromJID), "blah"
> );
> >
> > should have \(...) replaced with [ ... ] :
> >
> > reply_multi( \$daemon{xmpp_o}, [$adminuser{fromJID}, $fromJID], "blah" );
> >
> > because
> >
> > \('foo', 'bar')
> >
> > evaluates to
> >
> > (\'foo', \'bar')
> >
> > Does that clarify this for you?
>
> Not really ... I vaguely thought that [] might do the trick, but
> since when is an array in perl declared with [] instead of ()? You
> can also do
>
>
> foreach my $foo ('bar', 'baz') {
> print "$foo\n";
> }
>
>
> since foreach takes arrays. So why aren't arrays created the same way
> when being passed as parameters to functions (when signatures are
> being used)?
>
> Somehow I don't remember what [] exactly do there :/ If that's like
>
>
> foreach my $foo (@['bar', 'baz'])
>
>
> I'd guess I remember correctly and it would kinda make sense ...
>
> But still ... Parentheses should take precendence, shouldn't they?
>
>
> > Andrew
> >
> > On Sat, Jan 13, 2024 at 2:51 PM hw <[email protected]> wrote:
> >
> > > Hi,
> > >
> > > how do I pass an array that is created on the fly as one parameter of
> > > a function?
> > >
> > > Example:
> > >
> > >
> > > use feature 'signatures';
> > > no warnings 'experimental::signatures';
> > >
> > > sub reply_multi ( $xmpp_o, $rcpts, $msg ) {
> > > foreach my $rcpt (@$rcpts) {
> > > $$xmpp_o->MessageSend( type => 'chat', to => $rcpt, body =>
> $msg );
> > > }
> > >
> > > return;
> > > }
> > >
> > > reply_multi( \$daemon{xmpp_o}, \($adminuser{fromJID}, $fromJID),
> "blah" );
> > >
> > >
> > > This gives me an error at runtime: "Too many arguments for subroutine
> > > 'main::reply_multi' (got 4; expected 3)".
> > >
> > > Yeah, sure, ok, but is that even right? Or is signatures too
> > > experimental to handel that yet? Or how do I do what I want here?
> > >
> > >
> > > --
> > > To unsubscribe, e-mail: [email protected]
> > > For additional commands, e-mail: [email protected]
> > > http://learn.perl.org/
> > >
> > >
> > >
>
>
>
>
> --
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> http://learn.perl.org/
>
>
>