Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

passing arguments to subroutine

by c (Hermit)
on Jul 23, 2001 at 21:58 UTC ( #99103=perlquestion: print w/replies, xml ) Need Help??

c has asked for the wisdom of the Perl Monks concerning the following question:

i am in need of a head check. mine hurts and i am afraid my code is the cause.
i am trying to pass an argument to a subroutine i have built that generates a .htaccess file. my question is whether or not i am calling the right variable as it is passed to my sub.

my @webhost = glob("$www/*"); if ($panel) { for my $i(@webhost) { if ( -d $i ) && ( $i ne "lost+found" ) && ( $i ne "DEFUNCT" ) { if ( -d $i/controlpanel ) { system("cp -fRp $dir $i"); } else { mkdir $i/controlpanel/; system("cp -fRp $dir $i"); } } htaccess($i); } } sub htaccess { my $fqdn = shift; open(FH,">/www/$fqdn/controlpanel/.htaccess"); print<<HTA; AuthUserFile /etc/htsec/$fqdn.ht AuthName "Hosting Administration" AuthType Basic require valid-user HTA close(FH); }

my biggest concern is that the $fqdn is being set to the $i value without any gotchas, e.g. do i need to chomp this bad boy to create the file or any other ghosts i may not be seeing. also, i am using shift to pull the value out of @_ in my subroutine. is this the right way to do this? granted in this example $i is a single value rather than multiple strings being passed to the sub-routine, but i am sure i am going to make use of this again in the near future.

humbly -c

Replies are listed 'Best First'.
Re: passing arguments to subroutine
by grinder (Bishop) on Jul 23, 2001 at 22:16 UTC
    You don't need to chomp. If you printed the variable out you would see it is so. You will also need to interpolate $i/controlpanel in a string context.

    I would rewrite the copying code as follows:

    mkdir $i/controlpanel/ unless -d "$i/controlpanel"; system 'cp', '-fRp', $dir, $i" and die "cp went boom ($?).\n";

    When oh when will people check for system errors?

    shift is as good a way as any for reading the arguments passed to a routine.

    update: hmm, the fact that the mkdir call is not checked is left as an exercise to the reader (it also needs a protection mask la 0500).

    --
    g r i n d e r
      More than one mistake in that code. I'd like to say that I left that mkdir check out because the code is still in the beginning stages, but I like to think of myself as an honest guy.

      mkdir("$i/controlpanel", 0755) or die "Problems making dir : $!\n" unl +ess ( -d "$i/controlpanel" ); system("cp -fRp $dir/controlpanel") or die "Problems copying dir : $!\ +n";

      better?

      humbly -c

        All the more reason for you to check your mkdir if the code is in it's beginning stages. You want to throw all the sanity checking devices that you can think of in your code (especially beta code).

        redmist
        Silicon Cowboy
        Change:
        system("cp -fRp $dir/controlpanel") or die "Problems copying dir : $!\ +n";
        To:
        system("cp -fRp $dir/controlpanel") == 0 or die "Problems copying dir +: $!\n";
        system returns the exit status of the program being run, and the programs conventionally return 0 (zero) on success.
(tye)Re: passing arguments to subroutine
by tye (Sage) on Jul 24, 2001 at 01:52 UTC
    my @webhost = glob("$www/*"); # ... for my $i(@webhost) { if ( -d $i ) && ( $i ne "lost+found" ) && ( $i ne "DEFUNCT" ) {

    $i will never equal "lost+found" since $i will always start with "$www/". You might want File::Basename.

            - tye (but my friends call me "Tye")

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://99103]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2019-06-19 13:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Is there a future for codeless software?



    Results (88 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!