Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

pushd and popd

0 views
Skip to first unread message

Shishir K. Singh

unread,
Jul 23, 2002, 2:15:03 PM7/23/02
to begi...@perl.org
I was just wondering if there is anything similar in perl for unix commands pushd / popd ??

Thanks
Shishir

James Kipp

unread,
Jul 23, 2002, 2:22:06 PM7/23/02
to begi...@perl.org
how about functions like push, pop, shift, unshift ?

> --
> To unsubscribe, e-mail: beginners-...@perl.org
> For additional commands, e-mail: beginne...@perl.org
>
>

Jeff 'Japhy' Pinyan

unread,
Jul 23, 2002, 2:24:35 PM7/23/02
to Shishir K. Singh, begi...@perl.org
On Jul 23, Shishir K. Singh said:

>I was just wondering if there is anything similar in perl for unix
>commands pushd / popd ??

I can't find such a thing. You (or someone else) could write one. It
doesn't seem too difficult; it's just an array.

--
Jeff "japhy" Pinyan ja...@pobox.com http://www.pobox.com/~japhy/
RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
[ I'm looking for programming work. If you like my work, let me know. ]

Shishir K. Singh

unread,
Jul 23, 2002, 2:25:27 PM7/23/02
to Kipp, James, begi...@perl.org
hmmm..they are array functions...I guess I can use the cwd/push/pop to simulate the pushd and popd, just was being lazy :) and wanted to know if a direct pushd popd kind of function exists!!


>how about functions like push, pop, shift, unshift ?

> -----Original Message-----
> From: Shishir K. Singh [mailto:sks...@synapsistech.com]
> Sent: Tuesday, July 23, 2002 2:15 PM
> To: begi...@perl.org
> Subject: pushd and popd
>
>

> I was just wondering if there is anything similar in perl for
> unix commands pushd / popd ??
>

Shishir K. Singh

unread,
Jul 23, 2002, 2:26:34 PM7/23/02
to ja...@pobox.com, begi...@perl.org
>I was just wondering if there is anything similar in perl for unix
>commands pushd / popd ??

>I can't find such a thing. You (or someone else) could write one. It


>doesn't seem too difficult; it's just an array.

Good Idea!! might as well do that!!

John W. Krahn

unread,
Jul 23, 2002, 2:29:43 PM7/23/02
to begi...@perl.org
"Shishir K. Singh" wrote:
>
> I was just wondering if there is anything similar
> in perl for unix commands pushd / popd ??

pushd and popd are built-in shell commands, they aren't really "Unix"
commands. What exactly are you trying to do?

perldoc -f push
perldoc -f pop
perldoc -f shift
perldoc -f unshift
perldoc -f splice


John
--
use Perl;
program
fulfillment

Shishir K. Singh

unread,
Jul 23, 2002, 2:39:20 PM7/23/02
to John W. Krahn, begi...@perl.org
>
> I was just wondering if there is anything similar
> in perl for unix commands pushd / popd ??

> pushd and popd are built-in shell commands, they aren't really "Unix"
>commands. What exactly are you trying to do?

>perldoc -f push
>perldoc -f pop
>perldoc -f shift
>perldoc -f unshift
>perldoc -f splice

I have this awfully old shell script that used lots of pushd and popd and I need to convert it to perl. I will have to settle with push and pop and cwd for the time being. Thanks anyways !!

John W. Krahn

unread,
Jul 23, 2002, 3:14:35 PM7/23/02
to begi...@perl.org


Here is a very simple implementation (no error checking.) Stick it at
the top of your program so that the prototypes will work.

{
use Cwd;
my @stack = cwd;

sub dirs () {
print "@stack\n";
}
sub pushd ($) {
unshift @stack, shift;
dirs;
}
sub popd () {
@stack > 1 and shift @stack;
dirs;

Shishir K. Singh

unread,
Jul 23, 2002, 3:17:41 PM7/23/02
to John W. Krahn, begi...@perl.org
Thanks John...I appreciate it!!

Drieux

unread,
Jul 23, 2002, 3:18:37 PM7/23/02
to begin begin

On Tuesday, July 23, 2002, at 11:39 , Shishir K. Singh wrote:
[..]

> I have this awfully old shell script that used lots of pushd and popd and
> I need to convert it to perl. I will have to settle with push and pop and
> cwd for the time being. Thanks anyways !!

traditionally pushd/popd are csh/tcsh things for walking around
the directory structure - you may have multiple issues that
you will want to think about as you port over this old
csh/tcsh script....

some of the 'modern' forms of /bin/sh also use this, but
of course you do not 'inherit' that "stack" in any of the
sub processes....

So you may want to check why you are pushd/popd-ing all
over the place in the first place - and may wish to think
about re-organizing this so that a simple chain of movement
would suffice:

my @dir_run = qw( first_dir second_dir )

so that you merely pop them off that array - or reference
them by index.....

ciao
drieux

---

John W. Krahn

unread,
Jul 23, 2002, 3:53:40 PM7/23/02
to begi...@perl.org
"John W. Krahn" wrote:
>
> "Shishir K. Singh" wrote:
> > >
> > > I was just wondering if there is anything similar
> > > in perl for unix commands pushd / popd ??
> >
> > > pushd and popd are built-in shell commands, they aren't really "Unix"
> > >commands. What exactly are you trying to do?
> >
> > >perldoc -f push
> > >perldoc -f pop
> > >perldoc -f shift
> > >perldoc -f unshift
> > >perldoc -f splice
> >
> > I have this awfully old shell script that used lots of pushd
> > and popd and I need to convert it to perl. I will have to
> > settle with push and pop and cwd for the time being. Thanks
> > anyways !!
>
> [snip]


Sorry, I forgot to change to the directory being pushed. :-) This
should work a lot better.

{
use Cwd;
my @stack = cwd;

sub dirs () {
print "@stack\n";
}
sub pushd ($) {

unless ( chdir $_[0] ) {
warn "Error: $!";
return;
}
unshift @stack, cwd;


dirs;
}
sub popd () {
@stack > 1 and shift @stack;

chdir $stack[0];

0 new messages