LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   perl simple recursive sub (https://www.linuxquestions.org/questions/programming-9/perl-simple-recursive-sub-863147/)

sharky 02-16-2011 03:20 PM

perl simple recursive sub
 
This is my sub;

Code:

sub projName
{
    print "Enter project name: " ;
    $PROJ = <STDIN>;
    print "";
    if (-d "${PROJ}")
    {
        print "Good project\n";
    }
    else
    {
        print "Not a valid project name. Try again\n" ;
        projName;
    }
}

I can call this routine and it works fine when I enter a valid name for $PROJ. If I enter an invalid name it goes to the else block and prints the statement. However, it does not call itself. Instead the script just exits.

I've googled 'perl recursive subroutines' and the example don't appear to be doing anything different.

Any ideas what I'm doing wrong?

Tinkster 02-16-2011 03:41 PM

Not sure what your problem is ...
Code:

#!/usr/bin/perl -w
use strict;

sub projName;

projName;

sub projName
{
  my $PROJ;
  print "Enter project name: " ;
  $PROJ = <STDIN>;
  chomp $PROJ;
  print "";
  if (-d "${PROJ}")
  {
    print "Good project\n";
  }
  else
  {
    print "Not a valid project name. Try again\n" ;
    projName;
  }
}

Code:

$ perl sub.pl
Enter project name: blah
Not a valid project name. Try again
Enter project name: yadda
Not a valid project name. Try again
Enter project name: mangle   
Good project

works as designed.


Cheers,
Tink

P.S.: I'm moving this over to programming where it's better suited.

sharky 02-16-2011 04:16 PM

Quote:

Originally Posted by Tinkster (Post 4260743)
Not sure what your problem is ...
Code:

#!/usr/bin/perl -w
use strict;

sub projName;

projName;

sub projName
{
  my $PROJ;
  print "Enter project name: " ;
  $PROJ = <STDIN>;
  chomp $PROJ;
  print "";
  if (-d "${PROJ}")
  {
    print "Good project\n";
  }
  else
  {
    print "Not a valid project name. Try again\n" ;
    projName;
  }
}

Code:

$ perl sub.pl
Enter project name: blah
Not a valid project name. Try again
Enter project name: yadda
Not a valid project name. Try again
Enter project name: mangle   
Good project

works as designed.


Cheers,
Tink

P.S.: I'm moving this over to programming where it's better suited.

Hi Tinkster, I may have had several problems. I did not have a line "sub projName" in my script before the subroutine is written, I did not 'use strict', I did not declare 'my $PROJ' and I called projName after the subroutine in my script. Not sure which specifically made it work.

Thanks to you it's working now.

Regards,

sharky 02-16-2011 04:19 PM

I also didn't 'chomp $PROJ'. Without that it is never a valid name.

Tinkster 02-16-2011 04:28 PM

Quote:

Originally Posted by sharky (Post 4260785)
I also didn't 'chomp $PROJ'. Without that it is never a valid name.

Heh. Glad it's settled; just for future reference:
it's ALWAYS adivsable to do both the "-w" and "use strict;".

Helps avoiding head-aches in the long run.





Cheers,
Tink


All times are GMT -5. The time now is 07:08 AM.