LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-16-2011, 03:20 PM   #1
sharky
Member
 
Registered: Oct 2002
Posts: 569

Rep: Reputation: 84
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?
 
Old 02-16-2011, 03:41 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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.
 
Old 02-16-2011, 04:16 PM   #3
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
Quote:
Originally Posted by Tinkster View Post
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,
 
Old 02-16-2011, 04:19 PM   #4
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
I also didn't 'chomp $PROJ'. Without that it is never a valid name.
 
Old 02-16-2011, 04:28 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by sharky View Post
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

Last edited by Tinkster; 02-16-2011 at 04:29 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Recursive anonymous sub in Perl? MTK358 Programming 27 07-22-2010 01:42 PM
[SOLVED] Recursive difficulty in Perl Robhogg Programming 8 05-28-2010 12:45 PM
Got to be a better way. Perl recursive directory listing (not files) PB0711 Programming 8 03-16-2009 05:24 PM
Perl file handler in recursive function enemorales Programming 4 02-02-2009 03:20 PM
perl: simple one line replacement, simple error message! pwd_pwd_omg_pwd Programming 8 11-19-2008 12:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration