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

over loading

by saranperl (Initiate)
on Aug 19, 2009 at 09:26 UTC ( [id://789745]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: over loading
by Anonymous Monk on Aug 19, 2009 at 09:30 UTC
    Use strict warnings and diagnostics or die
    use strict; use warnings; use diagnostics; package pkgsam1; sub min{ print "min"; } sub min{ print "min11"; }
    Subroutine min redefined at test.pl line 8 (#1) (W redefine) You redefined a subroutine. To suppress this warning +, say { no warnings 'redefine'; eval "sub name { ... }"; }
Re: over loading
by Taulmarill (Deacon) on Aug 19, 2009 at 09:31 UTC
    There is no overloading here. You just define a subroutine called min, and then you redefine it with different code.
    warnings.pm should also give you a warning about this.
Re: over loading
by biohisham (Priest) on Aug 19, 2009 at 11:10 UTC
    Suppose these two scenarios:
    • you are working on this program which has many variables, and you were a bit niggardly naming your variables in a nice, comprehensible concept, you ended up initializing the same variable twice unintentionally and with different values, your program starts working, but the results that're coming out are not what you expect. You could not see that coming because you did not use the strictures.
      The most chanted mantras in the Grand Monastery is "use strict, use warnings" .
    • You have a string variable, $string="hello", however, at some part of your program you wanted to carry out an operation that involves incrementation of a number variable, inadvertently you typed $string++ when you wanted to type $number++, the program worked, giving you a weird result, how would you detect that you were incrementing a string where you should've been incrementing a number unless you switched warnings on?
      The most chanted mantras in the Grand Monastery is "use strict, use warnings".
      • $number = 3; $number = 4; $string = "Hello"; $string ++ print "$var\n"; #would print 4 and overwrite 3. print "$string"; #prints "hellp" and you don't know why "hello" didn' +t print
        If you've used strict and wanrings instead Perl would ask you to intervene and check if there is something it thinks is suspicious "in this case the same variable having been set twice". I am just giving you a very simple example to illustrate this concept but this is the norm of the other data types in Perl too.

        The same concept can be stretched further to subroutines naming and conventions so be careful and happy Perl Programming :)

        (UPDATED): Made some amendment to cover two scenarios.

        Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.

      Except that $string++ not only doesn't generate errors or warnings, but is in fact a highly useful DWIMism of Perl:

      use warnings; use strict; my $letter = 'a'; print $letter++ for 1 .. 26; print "\n"; my $serial = 'S001'; printf "%s, ", $serial++ for 1 .. 10;

      Prints:

      abcdefghijklmnopqrstuvwxyz S001, S002, S003, S004, S005, S006, S007, S008, S009, S010,

      True laziness is hard work
Re: over loading
by JavaFan (Canon) on Aug 19, 2009 at 09:56 UTC
    Overloading can only happen in two cases. First is when an object of a class that implements overloading is used as argument to an overloaded operation. Second is when constants are overloaded.

    Your example is just a matter of replacing an old value with a new one. The second declaration of min replaces the first. Warnings would have told you.

Log In?
Username:
Password:

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

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

    No recent polls found