For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > May 2007 > Passing multiple mixed arguments to subs









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Passing multiple mixed arguments to subs
Ben Edwards

2007-05-24, 7:58 am

I am passing a reference to a hash ($publisher) and a array with an
unknown number of elements (@files). So the call is

delete_from_publishers( $publisher, @files )

Currently the beginning of the sub is:-

sub remove_files_from_ftp_server {
my $pub_detail = $_[0];
my $args = @_;
my @files = @_[1..($args-1)];

This works fine but is a bit messy. Is there a better way of acheving
the same result?

Regards,
Ben
--
Ben Edwards - Bristol, UK
If you have a problem emailing me use
http://www.gurtlush.org.uk/profiles.php?uid=4
(email address this email is sent from may be defunct)
Jeff Pang

2007-05-24, 7:58 am

Ben Edwards 写道:
> I am passing a reference to a hash ($publisher) and a array with an
> unknown number of elements (@files). So the call is
>
> delete_from_publishers( $publisher, @files )
>


mnnn,don't pass an original array to a subroutine at anytime.
Instead just pass a reference to routines.like,

delete_from_publishers($publisher,\@file
s);

then in the subroutine,

my $hash_ref = shift;
my $array_ref = shift;

This would make things more clear.


--
http://home.arcor.de/jeffpang/
Srinivas

2007-05-24, 7:58 am

Hi Ben,

You can use shift for this.

sub remove_files_from_ftp_server {
my $pub_detail = shift @_;
my @files = @_;

Thank You,

-srini

Ben Edwards wrote:
> I am passing a reference to a hash ($publisher) and a array with an
> unknown number of elements (@files). So the call is
>
> delete_from_publishers( $publisher, @files )
>
> Currently the beginning of the sub is:-
>
> sub remove_files_from_ftp_server {
> my $pub_detail = $_[0];
> my $args = @_;
> my @files = @_[1..($args-1)];
>
> This works fine but is a bit messy. Is there a better way of acheving
> the same result?
>
> Regards,
> Ben


Jeff Pang

2007-05-24, 7:58 am

Jeff Pang 写道:
> Ben Edwards 写道:
>
> mnnn,don't pass an original array to a subroutine at anytime.
> Instead just pass a reference to routines.like,
>
> delete_from_publishers($publisher,\@file
s);
>
> then in the subroutine,
>
> my $hash_ref = shift;
> my $array_ref = shift;
>
> This would make things more clear.
>
>


Also passing a reference to a routine is different in effect from
passing an original structure.See below,

# this would change the original array's values
$ perl -Mstrict -le 'my @x=(1,2,3);
> testx(\@x);
> print "@x";
> sub testx { my $re = shift; $re->[0]=111 }'

111 2 3

# but this wouldn't change original array's values
$ perl -Mstrict -le 'my @x=(1,2,3);
> testx(@x);
> print "@x";
> sub testx { my @x = @_;$x[0] =111 }'

1 2 3

--
http://home.arcor.de/jeffpang/
Paul Johnson

2007-05-24, 7:58 am

On Thu, May 24, 2007 at 05:55:13PM +0800, Jeff Pang wrote:
> Ben Edwards 写道:
>
> mnnn,don't pass an original array to a subroutine at anytime.


What, never?

http://pjcj-sings-opera.org/HMS-Pinafore.mp3

> Instead just pass a reference to routines.like,
>
> delete_from_publishers($publisher,\@file
s);
>
> then in the subroutine,
>
> my $hash_ref = shift;
> my $array_ref = shift;
>
> This would make things more clear.


Or perhaps not.

my ($publisher, @files) = @_;

--
Paul Johnson - paul@pjcj.net
http://www.pjcj.net
Jeff Pang

2007-05-24, 7:58 am

Paul Johnson 写道:
> On Thu, May 24, 2007 at 05:55:13PM +0800, Jeff Pang wrote:
>
> What, never?
>
> http://pjcj-sings-opera.org/HMS-Pinafore.mp3
>


Hmmmm!surely you can do anything you like,no one blame you.
I just suggest the better and clear way.


--
http://home.arcor.de/jeffpang/
Rob Dixon

2007-05-24, 9:58 pm

Jeff Pang wrote:
>
> Paul Johnson 写道:
>
>
> Hmmmm! surely you can do anything you like, no one blame you.
> I just suggest the better and clear way.


No Jeff, you suggested a way thought was better and at Paul and I disagree
with you.

Clearly you think printf() has been written wrongly? The call would be

printf "Value1: %d, Value2: %d\n", [42, 69];

if you had your way.

Rob
Brad Baxter

2007-05-25, 9:59 pm

On May 24, 5:36 am, funkyt...@gmail.com (Ben Edwards) wrote:
> I am passing a reference to a hash ($publisher) and a array with an
> unknown number of elements (@files). So the call is
>
> delete_from_publishers( $publisher, @files )
>
> Currently the beginning of the sub is:-
>
> sub remove_files_from_ftp_server {
> my $pub_detail = $_[0];
> my $args = @_;
> my @files = @_[1..($args-1)];
>
> This works fine but is a bit messy. Is there a better way of acheving
> the same result?
>
> Regards,
> Ben
> --
> Ben Edwards - Bristol, UK
> If you have a problem emailing me usehttp://www.gurtlush.org.uk/profiles.php?uid=4
> (email address this email is sent from may be defunct)


Is that a trick question? :-)

(As others have mentioned ...)

use warnings;
use strict;

my $publisher = "pub";
my @files = ( 1, 2, 3 );
delete_from_publishers( $publisher, @files );

#sub remove_files_from_ftp_server {
sub delete_from_publishers {
my( $publisher, @files ) = @_;
print "Publisher: $publisher\n";
print "Files: @files\n";
}

You probably want to read perldoc perlsub again.

Honestly, posting actual working code doesn't take that
much extra effort.

Cheers,

--
Brad

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com