Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Subroutines vs Modules

by sub_chick (Hermit)
on Dec 30, 2005 at 19:23 UTC ( [id://520048]=note: print w/replies, xml ) Need Help??


in reply to Subroutines vs Modules

After reading all of these helpful replies, I decided to try out a subroutine and attempt to better understand the syntax by adding comment:
#! /usr/bin/perl -w sub of_all_things { print "You called me!\n"; #will print this when called $one*$two; #Here are the workings that will return value } $one = 12; $two = 10; $three=&of_all_things; #return value $four=2*$three; #using the return value print "2 x 120 = $four\n";

OUTPUT:
You called me! 2 x 120 = 240

This seems to work out on my box, but I am most concerned as to whether my comments are aiming at in the right direction?


Es gibt mehr im Leben als Bücher, weißt du. Aber nicht viel mehr. - (Die Smiths)"

Replies are listed 'Best First'.
Re^2: Subroutines vs Modules
by Argel (Prior) on Dec 30, 2005 at 21:22 UTC
    That works but better practice (i.e. coding style, etc.) would be:
    #! /usr/bin/perl use strict; use warnings; #//////////////////////////////////////// sub whoami { print "Just entered subroutine ", (caller(1))[3], "\n"; return; } #//////////////////////////////////////// sub one_times_two { whoami(); my( $one, $two ) = @_; return $one * $two; } #//////////////////////////////////////// my $one = 12; my $two = 10; my $three = one_times_two( $one, $two ); my $four = 2 * $three; print "2 x $three = $four\n"; exit 0;
    With output:
    Just entered subroutine main::one_times_two 2 x 120 = 240
    Note: I beleive you need Perl 5.6 or greater for "use warnings".

    -- Argel

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-04-16 18:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found