For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2008 > reference to subroutine???









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 reference to subroutine???
Sanket Vaidya

2008-03-27, 4:16 am

Hi everyone,



Kindly go through the code below.



use strict;

use warnings;



sub hello;

my $ref = \&hello;

&{$ref};



sub hello

{

print "hello!!";

}



The output on perl 5.10 is

Hello!!



Whereas the output on perl 5.6.1 is

Hello!!1



Why two different outputs in two different versions?

From where did "1" come from & how to remove it?





Thanks in advance.



Regards,



Sanket Vaidya

Software Engineer

Patni Computer Systems Ltd.

A-78/9, GIDC Electronics Estate,

Sector 25, Gandhinagar - 382016

Tel: +91-79-2324 0905 Ext: 334

Mobile: +91-9824300897




Yitzle

2008-03-27, 4:16 am

Um. No offense, but can you double check that the same exact code is
being ran against both versions?
asmith9983@gmail.com

2008-03-27, 8:02 am

Hi I copied the code into a script so I could be sure it was the exactly the
same source being used on version 5.8.8 and 5.10.0. I'm running an AMD
Athlon64 x2 & Ubuntu 7.10 but that is I think irrelevant.
For those not familiar with vim line #2 turns off syntax colouring for this
file only.Its called a modeline feature.

--
Andrew in Edinburgh,Scotland

On Wed, 26 Mar 2008, yitzle wrote:

> Um. No offense, but can you double check that the same exact code is
> being ran against both versions?
>
>

Lawrence Statton

2008-03-27, 7:12 pm

sanket vaidya wrote:

>Whereas the output on perl 5.6.1 is
>
>Hello!!1


Ummm, beg to differ



#!/usr/bin/perl
use strict;
use warnings;
sub hello;

my $ref = \&hello;

&{$ref};


sub hello {
print "hello!!";
}


lawrence@sb1:~$ perl -l pbml.pl
hello!!
lawrence@sb1:~$ perl -v

This is perl, v5.6.1 built for i386-linux

Rob Dixon

2008-03-27, 7:12 pm

sanket vaidya wrote:
> Hi everyone,
>
> Kindly go through the code below.
>
> use strict;
> use warnings;
>
> sub hello;
> my $ref = \&hello;
> &{$ref};
>
> sub hello
> {
> print "hello!!";
> }
>
> The output on perl 5.10 is
> Hello!!
>
> Whereas the output on perl 5.6.1 is
> Hello!!1
>
> Why two different outputs in two different versions?
> From where did "1" come from & how to remove it?


I suspect the 1 is output by something separate from Perl. Try writing
the output to a file instead by rewriting your subroutine as:

sub hello {
open my $fh, '>', 'test' or die $!;
print $fh "Hello!!\n";
}

also, you should call the subroutine by reference like this:

my $ref = \&hello;
$ref->();

as the syntax you have chosen is prone to error.

HTH,

Rob
Sponsored Links







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

Copyright 2008 codecomments.com